/**
 * File name: MatrixDistanceMeter.java Author: Willard Wang Create date: Aug 24,
 * 2013
 */
package edu.sjtu.se.prophet.cluster;

import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * @author deathspeeder
 * 
 */
public class MatrixMapDistanceMeter implements DistanceMeter {
    private static Logger logger = Logger.getLogger(MatrixMapDistanceMeter.class);

    private byte[][] distanceMatrix;
    private HashMap<Point, Integer> indexMap = new HashMap<Point, Integer>();

    public MatrixMapDistanceMeter(Point[] points, byte[][] distanceMatrix) {
        for (int i=0; i<points.length; i++) {
            indexMap.put(points[i], i);
        }
        this.distanceMatrix = distanceMatrix;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * edu.sjtu.se.prophet.cluster.DistanceMeter#distance(edu.sjtu.se.prophet
     * .cluster.Point, edu.sjtu.se.prophet.cluster.Point)
     */
    @Override
    public int distance(Point a, Point b) {
        if (!indexMap.containsKey(a)) {
            logger.error("Point not in map " + a);
            return 0;
        }
        if (!indexMap.containsKey(b)) {
            logger.error("Point not in map " + b);
            return 0;
        }
        return distanceMatrix[indexMap.get(a)][indexMap.get(b)];
    }

}
